#include <iostream>
#include "DoubleLinkList.h"
#include "LinkStack.h"
// 表达式求值
//  (1 + 2) * 3 =

bool IsOper(char c)
{
    switch (c)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '(':
    case ')':
    case '=':
        return true;
    default:
        return false;
    }
}

bool IsNum(char c)
{
    if (c >= '0' && c <= '9')
        return true;
    return false;
}

int Operation(int prev, int next, char symbol)
{
    switch (symbol)
    {
    case '+':
        return prev + next;
        break;
    case '-':
        return prev - next;
        break;
    case '*':
        return prev * next;
        break;
    case '/':
        if (next == 0)
        {
            exit(-1); // 退出程序
        }
        return prev / next;
        break;
    default:
        exit(-1);
    }
}

char Precede(char ch1, char ch2)
{
    int row = 0, col = 0;
    switch (ch1)
    {
    case '+':row = 0;break;
    case '-':row = 1;break;
    case '*':row = 2;break;
    case '/':row = 3;break;
    case '(':row = 4;break;
    case ')':row = 5;break;
    case '=':row = 6;break;
    default:
        break;
    }
    switch (ch2)
    {
    case '+':col = 0;break;
    case '-':col = 1;break;
    case '*':col = 2;break;
    case '/':col = 3;break;
    case '(':col = 4;break;
    case ')':col = 5;break;
    case '=':col = 6;break;
    default:
        break;
    }

    char pre[7][7] = 
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=','0'},
        {'>','>','>','>','0','>','>'},
        {'<','<','<','<','<','0','='}
    };
    return pre[row][col];
}

int main()
{
    LinkStack NumS;  //数据栈
    LinkStack OperS; //运算符栈

    OperS.Push('=');

    char str[64] = {0};
    scanf("%[^\n]",str);

    int i = 0;
    while(str[i]!='\0')
    {
        //数字
        if(IsNum(str[i])) 
        {
            int num = 0;
            while(IsNum(str[i]))
            {
                num = 10 * num + str[i] - '0';
                i++;
            }
            std::cout<<"num: "<<num << std::endl; 
            NumS.Push(num);
        }  
        else if(IsOper(str[i]))
        {
            char pre = Precede(*OperS.GetTop(),str[i]);
            switch (pre)
            {
            case '>':
            {
                int next = *NumS.GetTop();
                NumS.Pop();
                int prev = *NumS.GetTop();
                NumS.Pop();
                char op = *OperS.GetTop();
                OperS.Pop();
                NumS.Push(Operation(prev,next,op));
                break;
            }
            case '<':
            {
                OperS.Push(str[i]);
                i++;
                break;
            }
            case '=':
            {
                OperS.Pop();
                i++;
                break;
            }
            default:
            {
                std::cout<<"表达式有误!\n";
                exit(-1);
                break;
            }
            }
        }
        else if(str[i] == ' ')
        {
            while(str[i] == ' ')
                i++;
        }                                          
    }

    std::cout <<"答案是: " <<*NumS.GetTop() << std::endl;

    return 0;
}